package com.ruoyi.statisticalCenter.controller;

import cn.hutool.core.net.URLEncoder;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.cache.CustomCacheFilter;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.logistic.utils.TableUtils;
import com.ruoyi.statisticalCenter.domain.ScHqIncomeAndExpendCollect;
import com.ruoyi.statisticalCenter.domain.vo.DateStrVo;
import com.ruoyi.statisticalCenter.domain.vo.ScHqIncomeAndExpendCollectExportVo;
import com.ruoyi.statisticalCenter.domain.vo.ScHqIncomeAndExpendCollectPointVo;
import com.ruoyi.statisticalCenter.domain.vo.ScHqIncomeAndExpendCollectVo;
import com.ruoyi.statisticalCenter.service.ScHqIncomeAndExpendCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sc/hqIncomeAndExpend")
@Api(tags = "统计中心-总部收支汇总报表")
public class ScHqIncomeAndExpendCollectController {
    @Autowired
    private ScHqIncomeAndExpendCollectService scHqIncomeAndExpendCollectService;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private TableService tableService;

    //@CustomCacheFilter
    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody ScHqIncomeAndExpendCollectVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        List<ScHqIncomeAndExpendCollect> list = scHqIncomeAndExpendCollectService.collect(vo);
        List<TableVo> fieldList = getFieldAnnotation(new ScHqIncomeAndExpendCollect());
        return TableUtils.handleListResponse(list, fieldList, "暂无数据");
    }

    @ApiOperation("cache")
    @PostMapping("cache")
    public R cache(@RequestBody DateStrVo time) {
        return scHqIncomeAndExpendCollectService.cache(time);
    }



    public List<TableVo> getFieldAnnotation(ScHqIncomeAndExpendCollect object) {
        Field[] fields = object.getClass().getDeclaredFields();
        List<TableVo> list = new ArrayList<>();
        TableVo tableVo = new TableVo();
        for (Field field : fields) {
            // 是否引用ApiModelProperty注解
            boolean bool = field.isAnnotationPresent(ApiModelProperty.class);
            if (bool) {
                String value = field.getAnnotation(ApiModelProperty.class).value();
                tableVo = new TableVo();
                if ("feeName".equals(field.getName())) {
                    tableVo.setIsPoint(1);
                }
                tableVo.setFields(field.getName());
                tableVo.setTranslate(value);
                list.add(tableVo);
            }
        }
        return list;
    }


    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(@RequestBody ScHqIncomeAndExpendCollectExportVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        String fileName = "attachment;filename=" + URLEncoder.createDefault().encode("总部收支汇总报表", Charset.forName("utf-8")) + ".xlsx";
        response.setHeader("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        List<ScHqIncomeAndExpendCollect> list = scHqIncomeAndExpendCollectService.collectExport(vo ,outputStream);
//        ExcelUtil<ScHqIncomeAndExpendCollect> util = new ExcelUtil(ScHqIncomeAndExpendCollect.class);
//        util.exportExcel(response, list, "总部收支汇总报表");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        scHqIncomeAndExpendCollectService.collectExport(vo, outputStream);
    }

    @ApiOperation("点击跳转")
    @PostMapping("/point")
    public R point(@RequestBody ScHqIncomeAndExpendCollectPointVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        List<HqNetworkBillDetail> list = scHqIncomeAndExpendCollectService.point(vo);
        List<TableVo> fieldList = tableService.getFieldAnnotation(new HqNetworkBillDetail());
        return TableUtils.handleListResponse(list, fieldList, "暂无数据");
    }
}
